package org.taobao.ibatiside.ui.wizard;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import org.eclipse.emf.databinding.EMFDataBindingContext;
import org.eclipse.jface.databinding.wizard.WizardPageSupport;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.widgets.Composite;

public abstract class BatisCommonPage extends WizardPage implements PropertyChangeListener{
	protected BatisSettingModel model;
	protected EMFDataBindingContext bindingContext;
	public BatisCommonPage(String pageName) {
		super(pageName);
		model = BatisSettingModel.getActiveModel();
	}
	public EMFDataBindingContext getBindingContext() {
		return bindingContext;
	}
	@Override
	public void createControl(Composite parent) {
		initDataBinding();
		doCreateControl(parent);
		doBinding(bindingContext);
	}
	private void initDataBinding() {
		bindingContext = new EMFDataBindingContext();
		WizardPageSupport.create(this, bindingContext);
	}
	protected abstract void doCreateControl(Composite parent);
	protected void doBinding(EMFDataBindingContext bindingContext){
		
	}
	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		if (getWizard() != null && getWizard().getContainer() != null && getWizard().getContainer().getCurrentPage() != null){
			getWizard().getContainer().updateButtons();
		}
	}
	protected BatisCommonPage(String pageName, String title,
			ImageDescriptor titleImage) {
		super(pageName, title, titleImage);
	}
	@Override
	public void dispose() {
		if (bindingContext != null){
			bindingContext.dispose();
		}
		if (model != null)
			model.removePropertyChangeListener(this);
		super.dispose();
	}
}
